package com.example.demo.hutool.redis;

import org.apache.lucene.util.RamUsageEstimator;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.ArrayList;
import java.util.List;

public class ConstantCom {
    public static final int BATCH_SIZE = 1000;

    public static void main(String[] args) {
        JedisPool jedisPool = RedisTypeTest.getJedisPool();

        Jedis jedis = jedisPool.getResource();
        List<String> delKeys = new ArrayList<>();
        long start = System.currentTimeMillis();
        try {
            //模糊匹配的KEY
            String VEHICLE_MATCH_KEY = "*";
            //结束点标识
            String SCAN_CURSOR_INDEX = "0";
            //一次扫描多少条数据
            Integer SCAN_CURSOR_COUNT = 500000;
            //创建扫描参数实体类
            ScanParams scanParams = new ScanParams();
            //设置模糊匹配的key
            scanParams.match(VEHICLE_MATCH_KEY);
            //设置分片扫描的数量
            scanParams.count(SCAN_CURSOR_COUNT);

            //游标索引开始和结束的索引值
            String cursor = SCAN_CURSOR_INDEX;
            //游标循环移动操作
            do {
                //开始扫描
                ScanResult<String> sr = jedis.scan(cursor, scanParams);
                //获取扫描返回结果
                List<String> keyResultList = sr.getResult();
                if (!CollectionUtils.isEmpty(keyResultList)) {
                    delKeys.addAll(keyResultList);
                }
                //更新游标位置
                cursor = sr.getCursor();

                //cursor = sr.getStringCursor();
            } while (!SCAN_CURSOR_INDEX.equals(cursor));
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start + "时间");

        long l = RamUsageEstimator.sizeOf(delKeys);
//计算指定对象本身在堆空间的大小，单位字节
        long l1 = RamUsageEstimator.shallowSizeOf(delKeys);
//计算指定对象及其引用树上的所有对象的综合大小，返回可读的结果，如：2KB
        String s = RamUsageEstimator.humanSizeOf(delKeys);

        System.out.println(l);
        System.out.println(l1);
        System.out.println(s);

    }
}
